package com.lcx.controller;

import com.fasterxml.jackson.databind.util.ClassUtil;
import com.lcx.config.R;
import com.lcx.entity.Musics;
import com.lcx.entity.dto.Dic;
import com.lcx.entity.dto.SwiperDto;
import com.lcx.service.IMusicsService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2023-11-09
 */
@RestController
@RequestMapping("/musics")
public class MusicsController {
    @Autowired
    private IMusicsService musicsService;

    @GetMapping("/musicList")
    public R musicList () {
        return R.okResult(musicsService.list());
    }
    @GetMapping("/musicListDic")
    public R musicListDic () {
        List<Musics> list = musicsService.list();
        List<Dic> dicList = new ArrayList<>();
        for (Musics musics : list) {
            Dic dic = new Dic();
            dic.setValue(musics.getId());
            dic.setLabel(musics.getMusicName());
            dicList.add(dic);
        }
        return R.okResult(dicList);
    }
    @GetMapping("/swiperList")
    public R swiperList () {
        return R.okResult(musicsService.findSwiperList());
    }
    @PostMapping("/addFile")
    public R addMusic (@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        return R.okResult(musicsService.addMusic(request,file));
    }
    @PostMapping("/addMusic")
    public R addMusic (@RequestBody Musics musics) {
        musics.setMusicPath("metest/music/"+musics.getMusicPath());
        musics.setVideoPath("metest/video/"+musics.getVideoPath());
        return R.okResult(musicsService.saveOrUpdate(musics));
    }
    @GetMapping("/deleteMusic")
    public R deleteMusic (@RequestParam("id") Integer id) {
        return R.okResult(musicsService.removeById(id));
    }
}
